##################################################
#### Rachel Brewster & Adam Chilton 	    ####
####                                          ####
#### Supplying Compliance:                    ####
#### Why and When the United States 	    ####
#### Complies with WTO Rulings                ####
#### Replication Code                         ####
#### 		         				    ####
#### 04/22/2014                               ####
##################################################


##################################################################
####### Setting Up R 							  		  ########
##################################################################


### Installing Libraries ###
## Note: This code only works with Zelig 3.5. 
##		  (I believe) this only works on a Mac. 
##       To run, reinstall Zelig using the code below. 
##       Before using, you must close and restart R. 

install.packages("Zelig", repos = "http://r.iq.harvard.edu/archived/", type = "source")

### Loading Libraries ###
library(Zelig)


#### Loading Data ####
data <- read.csv("2014-04-24_Supply Side_Data.csv", sep=",", header=TRUE)
dim(data)
names(data)
head(data)


#### Subsetting the Data ####

data2 <- data[data$subset_compliance==1,]
dim(data2)

data3 <- data[data$subset_comply_time_settled==1,]
dim(data3)

data4 <- data[data$subset_total_time_settled==1,]
dim(data4)



##################################################################
####### Table 1: Summary of Dependent Variable Collection ########
##################################################################

#Consolidated or Repeat Cases
#The total is 23 (11+6+8)
table(data$num_dispute)

#All other values
table(data$case_category)


##################################################################
####### Table 2: Summary of Dependent Variable Collection ########
##################################################################

#Breakdown Between Compliant and Non-Compliant Cases
table(data2$non_comply_dummy)

#Number of Days for Complaint Cases - The Mean is Reported
data_compliant_cases <- data2[data2$non_comply_dummy==0,]
summary(data_compliant_cases$total_days)

#Number of Days for Non-Complaint Cases - The Mean is Reported 
data_non_compliant_cases <- data2[data2$non_comply_dummy==1,]
summary(data_non_compliant_cases$total_days)

#Number of Days Total - The Mean is Reported 
summary(data2$total_days)

################################################
####### Appendix A: Summary Statistics  ########
################################################

#Congress Required
summary(data2$congress_required)
sd(data2$congress_required)

#Divided Government
summary(data2$divided_government)
sd(data2$divided_government)

#USA Exports
summary(data2$usa_exports_ln)
sd(data2$usa_exports_ln)

#Formal Alliance
summary(data2$cow_type1_dummy)
sd(data2$cow_type1_dummy)

#GDP Per Capita
summary(data2$gdppc_ln)
sd(data2$gdppc_ln)

#Population
summary(data2$population_ln)
sd(data2$population_ln)

#Polity Score
summary(data2$polity2)
sd(data2$polity2)

#Trade Remedy Case
summary(data2$trade_remedy_dummy)
sd(data2$trade_remedy_dummy)

#Contributions
summary(data2$contribution_2010_ln)
sd(data2$contribution_2010_ln)


#################################################################################
####### Appendix B.1: Regressopm Results for Figure 1                    ########
#################################################################################

##### Model 1 ####
z.out1 <- zelig(non_comply_dummy ~ congress_required,model ="logit", data=data2)
print(summary(z.out1), digits = 2)

#### Model 2 ####
z.out1 <- zelig(non_comply_dummy ~ congress_required+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="logit", data=data2)
print(summary(z.out1), digits = 2)

#### Model 3 ####
z.out1 <- zelig(non_comply_dummy ~ congress_required+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy+contribution_2010_ln,model ="logit", data=data2)
print(summary(z.out1), digits = 2)



#################################################################################
####### Appendix B.2: Regression Results for Figure 2                    ########
#################################################################################

### Note: Cox PH Models Are Not Compatible with the Zelig 4.0 Release as of 2/23/2013
###       Replicating this code may require reinstalling Zelig 3.5. To do so, run this code:
###       install.packages("Zelig", repos = "http://r.iq.harvard.edu/archived/", type = "source")library(Zelig)
###		  After doing so, close and restart R. I believe that this may only work on a Mac. 

##### Model 1 ####
z.out1 <- zelig(Surv(total_days, censored) ~ congress_required,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)

##### Model 2 ####
z.out1 <- zelig(Surv(total_days, censored) ~ congress_required+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)

##### Model 3 ####
z.out1 <- zelig(Surv(total_days, censored) ~ congress_required++divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy+contribution_2010_ln,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)




#############################################################################
####### Robustness Check # 1: Changing Congress Required 			 ########
#############################################################################

#### Figure 1 - Changing Helms Burton ####

z.out1 <- zelig(non_comply_dummy ~ congress_required_helms_burton,model ="logit", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(non_comply_dummy ~ congress_required_helms_burton+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="logit", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(non_comply_dummy ~ congress_required_helms_burton+contribution_2010_ln+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="logit", data=data2)
print(summary(z.out1), digits = 2)


#### Figure 2 - Changing Helms Burton ####

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required_helms_burton,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required_helms_burton+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required_helms_burton+contribution_2010_ln+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)


#### Figrue 1 - Changing Softwood Lumber ####

z.out1 <- zelig(non_comply_dummy ~ congress_required_lumber,model ="logit", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(non_comply_dummy ~ congress_required_lumber+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="logit", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(non_comply_dummy ~ congress_required_lumber+contribution_2010_ln+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="logit", data=data2)
print(summary(z.out1), digits = 2)


#### Figure 2 - Changing Softwood Lumber ####

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required_lumber,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required_lumber+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required_lumber+contribution_2010_ln+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)


#### Figure 1 - Changing Zeroing ####

z.out1 <- zelig(non_comply_dummy ~ congress_required_zeroing,model ="logit", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(non_comply_dummy ~ congress_required_zeroing+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="logit", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(non_comply_dummy ~ congress_required_zeroing+contribution_2010_ln+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="logit", data=data2)
print(summary(z.out1), digits = 2)


#### Figure 2 - Changing Zeroing ####

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required_zeroing,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required_zeroing+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required_zeroing+contribution_2010_ln+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)


#### Figure 1: Changing All (Helms Burton, Softwood Lumber, Zeroing) ####

z.out1 <- zelig(non_comply_dummy ~ congress_required_complete,model ="logit", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(non_comply_dummy ~ congress_required_complete+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="logit", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(non_comply_dummy ~ congress_required_complete+contribution_2010_ln+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="logit", data=data2)
print(summary(z.out1), digits = 2)


#### Figure 2: Changing All (Helms Burton, Softwood Lumber, Zeroing) ####

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required_complete,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required_complete+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required_complete+contribution_2010_ln+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)




###############################################################################
####### Robustness 2: Compliance Models w/o Zeroing counted as Compliant ######
###############################################################################


z.out1 <- zelig(non_comply_dummy_zeroing ~ congress_required,model ="logit", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(non_comply_dummy_zeroing ~ congress_required+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="logit", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(non_comply_dummy_zeroing ~ congress_required+contribution_2010_ln+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="logit", data=data2)
print(summary(z.out1), digits = 2)


###############################################################################
####### Robustness 3: Including Settled Cases 							 ######
###############################################################################

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required,model ="coxph", data=data4)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="coxph", data=data4)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required+contribution_2010_ln+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="coxph", data=data4)
print(summary(z.out1), digits = 10)

###############################################################################
####### Robustness 4: "Largest" Country									 ######
###############################################################################


####### Figure 1 - Largest ########

#Model 2
z.out1 <- zelig(non_comply_dummy ~ congress_required+divided_government+cow_type1_dummy+usa_exports_largest_ln+gdppc_largest_ln+population_largest_ln+polity2_largest+trade_remedy_dummy,model ="logit", data=data2)
print(summary(z.out1), digits = 2)

#Model 3 #
z.out1 <- zelig(non_comply_dummy ~ congress_required+contribution_2010_ln+divided_government+cow_type1_dummy+usa_exports_largest_ln+gdppc_largest_ln+population_largest_ln+polity2_largest+trade_remedy_dummy,model ="logit", data=data2)
print(summary(z.out1), digits = 2)



####### Figure 2 - Largest ########


z.out1 <- zelig(Surv(total_days, censored) ~ congress_required+divided_government+cow_type1_dummy+usa_exports_largest_ln+gdppc_largest_ln+population_largest_ln+polity2_largest+trade_remedy_dummy,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required+contribution_2010_ln+divided_government+cow_type1_dummy+usa_exports_largest_ln+gdppc_largest_ln+population_largest_ln+polity2_largest+trade_remedy_dummy,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)



###############################################################################
####### Robustness 5: Compliance Time									 ######
###############################################################################


z.out1 <- zelig(Surv(comply_days, censored) ~ congress_required,model ="coxph", data=data3)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(Surv(comply_days, censored) ~ congress_required+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="coxph", data=data3)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(Surv(comply_days, censored) ~ congress_required+contribution_2010_ln+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="coxph", data=data3)
print(summary(z.out1), digits = 2)


#################################################################
####### Robustness #6: Probit for Figure 1 Instead of Logit #####
#################################################################

z.out1 <- zelig(non_comply_dummy ~ congress_required,model ="probit", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(non_comply_dummy ~ congress_required+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="probit", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(non_comply_dummy ~ congress_required+contribution_2010_ln+divided_government+cow_type1_dummy+usa_exports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="probit", data=data2)
print(summary(z.out1), digits = 2)



#################################################################
####### Robustness 7: Using USA Imports Instead of Exports ######
#################################################################

####### Figure 1 - Imports ###########

z.out1 <- zelig(non_comply_dummy ~ congress_required,model ="logit", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(non_comply_dummy ~ congress_required+divided_government+cow_type1_dummy+usa_imports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="logit", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(non_comply_dummy ~ congress_required+contribution_2010_ln+divided_government+cow_type1_dummy+usa_imports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="logit", data=data2)
print(summary(z.out1), digits = 2)


########### Figure 2 - Imports ###########

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required+divided_government+cow_type1_dummy+usa_imports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)

z.out1 <- zelig(Surv(total_days, censored) ~ congress_required+contribution_2010_ln+divided_government+cow_type1_dummy+usa_imports_ln+gdppc_ln+population_ln+polity2+trade_remedy_dummy,model ="coxph", data=data2)
print(summary(z.out1), digits = 2)

